﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Sales
{
    public partial class Checks : Form
    {
        private ToolStripMenuItem menuItem = null;

        public Checks()
            : this(null)
        {
        }

        public Checks(ToolStripMenuItem menuItem)
        {
            InitializeComponent();

            this.menuItem = menuItem;
            if (menuItem != null)
                menuItem.Tag = this;
        }

        private void Checks_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (menuItem != null)
                menuItem.Tag = null;
        }
        //-------------------------------------------------------------------------------------------------
        private void Checks_Load(object sender, EventArgs e)
        {
            toolStripButtonView.Enabled = false;
            toolStripRefresh_Click(toolStripButtonRefresh, EventArgs.Empty);
            dateTimePicker1.Value = DateTime.Today;
            if (!DIProject.Util.Setting.IsOffice)
                dateTimePicker1.Enabled = false;
            dateTimePicker2.Value = DateTime.Today.AddDays(1);
        }
        //-------------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------
        private void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            checksBindingSource.Filter = string.Format
            ("DocDate >= #{0}# and DocDate <= #{1}#"
            , dateTimePicker1.Value.ToString(System.Globalization.DateTimeFormatInfo.InvariantInfo)
            , dateTimePicker2.Value.ToString(System.Globalization.DateTimeFormatInfo.InvariantInfo)
            );
        }
        //-------------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------
        private void toolStripRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                checksTableAdapter.Fill(sales.Checks);
            }
            catch (SqlException eq)
            {
                MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        //-------------------------------------------------------------------------------------------------
        private void toolStripView_Click(object sender, EventArgs e)
        {
            if (checksGridView.CurrentCell != null)
            {
                Check check = new Check((DIProject.DataSets.Sales.ChecksRow)((DataRowView)checksGridView.CurrentCell.OwningRow.DataBoundItem).Row);
                check.ShowDialog(this);
            }
        }
        //-------------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (clickedCell != null)
            {
                viewToolStripMenuItem.Enabled = true;
            }
            else
            {
                viewToolStripMenuItem.Enabled = false;
            }
        }
        //-------------------------------------------------------------------------------------------------
        private DataGridViewCell clickedCell = null;
        private void checksGridView_MouseDown(object sender, MouseEventArgs e)
        {
            clickedCell = null;
            if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = checksGridView.HitTest(e.X, e.Y);
                if (hit.Type == DataGridViewHitTestType.Cell)
                    checksGridView.CurrentCell = clickedCell = checksGridView.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
            }
        }
        //-------------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------
        private void checksGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            if (checksGridView.CurrentCell != null)
            {
                toolStripButtonView.Enabled = true;
            }
            else
            {
                toolStripButtonView.Enabled = false;
            }
        }
        //-------------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------
        private void checksGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                e.Handled = true;
        }
        //-------------------------------------------------------------------------------------------------
        private void checksGridView_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r' && checksGridView.CurrentCell != null)
                toolStripView_Click(toolStripButtonView, EventArgs.Empty);
        }
        //-------------------------------------------------------------------------------------------------
        private void checksGridView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo hit = checksGridView.HitTest(e.X, e.Y);
            if (hit.Type == DataGridViewHitTestType.Cell)
            {
                checksGridView.CurrentCell = clickedCell = checksGridView.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
                toolStripView_Click(toolStripButtonView, EventArgs.Empty);
            }
        }
        //-------------------------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------------------------
    }
}